package yl.hs.bmipfull.service.salemanage.impl;

import com.fasterxml.jackson.core.JsonProcessingException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import yl.hs.bmipfull.daos.salemanage.IContractProductPriceAdjustDao;
import yl.hs.bmipfull.pojo.salemanage.ContractProductPriceAdjustRecordEntity;
import yl.hs.bmipfull.pojo.salemanage.SaleContractEntity;
import yl.hs.bmipfull.service.salemanage.IContractProductPriceAdjustService;
import yl.hs.bmipfull.utils.HsApp;
import yl.hs.bmipfull.utils.Tl;
import yl.hs.bmipfull.utils.datastate.ActionType;
import yl.hs.bmipfull.viewmodels.SubmitModel;

import java.util.Date;
import java.util.List;

/*
 * ContractProductPriceAdjustServiceImpl
 * @category
 * @author 谢景广
 * @date 2022-10-13
 */
@Service
public class ContractProductPriceAdjustServiceImpl extends SaleBaseService implements IContractProductPriceAdjustService {

    @Override
    public Integer changeToApproved(String sn) throws JsonProcessingException {
        var entity = dao.findBySN(sn);
        Integer result = 0;
        if(entity!=null) {
            var backJson = entity.toJson();
            entity.setState(1);
            result = dao.update(entity);
            if(result>0) {
                writeLog(ActionType.REVIEWED, entity.getSn(), null, backJson, entity.getClass());
            }
        }
        return result;
    }

    @Override
    public ContractProductPriceAdjustRecordEntity findContractAdjust(String contractSN) {
        return dao.findLastAdjust(contractSN);
    }

    @Override
    public List<ContractProductPriceAdjustRecordEntity> queryContractAdjust(String contractSN) {
        return dao.queryContractAdjust(contractSN);
    }

    @Override
    public Integer saveModel(SubmitModel model) throws Exception {
        var user = HsApp.getCurrent();
        Integer result = 0;
        if(Tl.isEmpty(model.getSn())){
            var entity = new ContractProductPriceAdjustRecordEntity();
            var changeLog = Tl.getChangeLog(entity,model.getData());
            entity.setSn(queryNewSN(ContractProductPriceAdjustRecordEntity.class));
            entity.setAdjuster(user.getUserName());
            entity.setAdjustUserSn(user.getUserSN());
            entity.setAdjustTime(new Date());
            result =  dao.insertNew(entity);
            writeLog(ActionType.CREATE,entity.getSn(),changeLog,null, entity.getClass());
        }
        else {
            var entity = dao.findBySN(model.getSn());
            var backJson = entity.toJson();
            var changeLog = Tl.getChangeLog(entity,model.getData());
            entity.setAdjustTime(new Date());
            result = dao.update(entity);
            writeLog(ActionType.UPDATE,entity.getSn(),changeLog,backJson, entity.getClass());
        }
        return result;
    }

    private IContractProductPriceAdjustDao dao;

    @Autowired
    public void setDao(IContractProductPriceAdjustDao dao) {
        this.dao = dao;
    }
}
